package com.alex.blog.service.impl;

import com.alex.blog.entity.Permissions;
import com.alex.blog.entity.Role;
import com.alex.blog.entity.User;
import com.alex.blog.service.LoginService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Service
public class LoginServiceImpl implements LoginService {

    @Override
    public User getUserByName(String getMapByName) {
        return getMapByName(getMapByName);
    }

    /**
     * 模拟数据库查询
     *
     * @param userName 用户名
     * @return User
     */
    private User getMapByName(String userName) {
        // 权限 query | add
        Permissions permissions1 = new Permissions("1", "query");
        Permissions permissions2 = new Permissions("2", "add");

        // 角色 admin 拥有权限 query | add
        Set<Permissions> permissionsSet = new HashSet<>();
        permissionsSet.add(permissions1);
        permissionsSet.add(permissions2);
        Role role01 = new Role("1", "admin", permissionsSet);

        // 角色 user 拥有权限  query
        Set<Permissions> permissionsSet1 = new HashSet<>();
        permissionsSet1.add(permissions1);
        Role role02 = new Role("2", "user", permissionsSet1);

        // 用户 wsl:123456 拥有角色 admin
        Set<Role> roleSet01 = new HashSet<>();
        roleSet01.add(role01);
        User user01 = new User("1", "wsl", "123456", roleSet01);

        // 用户 alex:123 拥有角色 user
        Set<Role> roleSet02 = new HashSet<>();
        roleSet02.add(role02);
        User user02 = new User("2", "alex", "123", roleSet02);

        Map<String, User> map = new HashMap<>();
        map.put(user01.getUserName(), user01);
        map.put(user02.getUserName(), user02);

        return map.get(userName);
    }
}
